package com.sitech.portal.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
 * 读取portal.properties文件，初始化系统属性
 * ClassName: com.sitech.ismp.util.context.InitApplicationPropertiesServlet
 * @author liujhc
 * @since Mar 7, 2011
 */
public class InitApplicationPropertiesServlet extends HttpServlet {

    public static final String PORTAL_PROPERTIES = "PORTAL_PROPERTIES";
    private String configLocation = "/portal.properties";

    @Override
    public void init() throws ServletException {
        String config = getServletConfig().getInitParameter("configLocation");
        if (config != null) {
            configLocation = config;
        }
        InputStream is = InitApplicationPropertiesServlet.class.getResourceAsStream(this.configLocation);
        if (is == null) {
            throw new ServletException("Properties file not found: " + this.configLocation);
        }
        try {
            Properties prop = new Properties();
            prop.load(is);
            getServletContext().setAttribute(PORTAL_PROPERTIES, prop);
        } catch (IOException ex) {
            throw new ServletException(ex);
        }
    }
}
